<template>
  <!-- 右侧栏 置顶 -->
  <div id="timeline" v-if="activities.length > 0">
    <div class="radio" style="margin-bottom: 8px;">
      <el-radio-group v-model="reverse">
        <el-radio :label="true">倒序</el-radio>
        <el-radio :label="false">正序</el-radio>
      </el-radio-group>
    </div>
    <el-timeline :reverse="reverse">
      <el-timeline-item v-for="(activity, index) in activities" :key="index"
        :timestamp="activity.createTime| formatTimer">
        <el-link :underline="false" @click="showDetail(activity.id)">{{ activity.title }}</el-link>
      </el-timeline-item>
    </el-timeline>
  </div>
</template>

<script>
import {
    articleQueryList
} from '@/api/article'

export default {
    name: 'timeline',
    data () {
        return {
            articles: [],
            reverse: true,
            activities: []
        }
    },
    filters: {
        formatTimer: function (value) {
            let date = new Date(value)
            let year = date.getFullYear()

            let month = date.getMonth() + 1
            month = month < 10 ? '0' + month : month

            let day = date.getDate()
            day = day < 10 ? '0' + day : day

            let hour = date.getHours()
            hour = hour < 10 ? '0' + hour : hour

            let minute = date.getMinutes()
            minute = minute < 10 ? '0' + minute : minute

            let second = date.getSeconds()
            second = second < 10 ? '0' + second : second

            console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second)
            return year + '-' + month + '-' + day
        }
    },
    methods: {
        showDetail (articleId) {
            this.$router.push({
                path: 'articleDetail',
                query: {
                    'articleId': articleId
                }
            })
        }
    },
    mounted () {
        articleQueryList({
            istimeline: true,
            limit: 10
        }).then((response) => {
            if (response && response.code === 200) {
                this.activities = response.data
            }
        })
    }
}
</script>

<style scoped>
#timeline {
  width: auto;
  height: 100%;
  margin: 10px 0px 0px 0px;
  padding: 5px;
  background-color: #fff;
}
</style>
